<?php
class Core_Common_Controllers_Plugins_Acl extends Zend_Controller_Plugin_Abstract {
	protected $_acl;
	
	public function init() {
		if(null===$this->_acl) {
			$cache = Core_Common_Cache::getInstance();
			$acl = $cache->acl;
			if(isset($acl)) {
				$this->_acl = $acl;
			}
			else {
				$this->_acl = new Core_Authentication_Acl();
				$cache->acl = $this->_acl;
			}
		}
	}
	
	public function preDispatch($request) {
		$this->init();
		$front = Zend_Controller_Front::getInstance();
		$auth = Zend_Auth::getInstance();
		$identity = $auth->getIdentity();
		
		if ($identity!==null && $identity instanceof Core_Business_User) {
			$rule = $auth->getIdentity()->getRole();
		}
		else {
			$rule = "guest";
		}
		
		$controller = $front->getRequest()->getControllerName();
		$module = $front->getRequest()->getModuleName();
		$action = $front->getRequest()->getActionNAme();
		if (!$this->_acl->isAllowed($rule,$controller,$action)) {
			$request->setControllerName("auth");
			$request->setModuleName("default");
			$request->setActionName("login");
		}
		
	}
}